import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';
export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async goToWeixinPay(openid: string) {
      const {
        main: {selectId, list},
      } = getData();
      const context = await api.SyntheticRechargeController.iplandPay({goodsType: 0, id: selectId, openid});
      //@ts-ignore
      const {timeStamp, nonceStr, signType, paySign} = context;
      await Taro.requestPayment({
        timeStamp,
        nonceStr,
        //@ts-ignore
        package: context.package,
        signType,
        paySign,
        success: async (res) => {
          Taro.showToast({
            title: '魔力币充值成功!',
            icon: 'none',
          });
          action.getMemberBase();
        },
      });
    },
    async reload() {
      const result = await api.SyntheticRechargeController.getRechargeList({});
      dispatch({
        type: Command.init,
        payload: {
          main: {
            list: result,
            selectId: result[0].id,
          },
        },
      });
      action.commonChange('main.list', result);
      action.commonChange('main.selectId', result[0].id);
    },
    async getMemberBase() {
      const result = await api.MemberController.getMemberBase();

      dispatch({
        type: Command.init,
        payload: {
          main: {
            memberBase: result,
            money: result.magicCoin,
          },
        },
      });
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageISyntheticRechargeMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
